package com.piece.generator.controller;

import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.web.controller.FrameWorkController;
import com.piece.module.generator.service.GenService;
import com.piece.module.generator.util.GenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags = {"代码自动生成公共管理"})
public class GenPublicController extends FrameWorkController {

    @Resource
    private GenService genService;

    @PostMapping("/public/jsonschema/pojo/preview/{sourceType}")
    @ApiOperation(value = "根据jsonschema生成实体类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "className", value = "类名称"),
            @ApiImplicitParam(name = "packageName", value = "包路径"),
            @ApiImplicitParam(name = "jsonschema", value = "jsonschema"),
            @ApiImplicitParam(name = "jpaAnnotationFlag", value = "jpaAnnotationFlag"),
            @ApiImplicitParam(name = "swaggerAnnotationFlag", value = "swaggerAnnotationFlag")
    })
    public AjaxResponse previewJsonSchema(@PathVariable String sourceType) {
        String className = getRequest().getParameter("className");
        String packageName = getRequest().getParameter("packageName");
        String jsonschema = getRequest().getParameter("jsonschema");
        boolean jpaAnnotationFlag = Convert.toBool(getRequest().getParameter("jpaAnnotationFlag"), false);
        boolean swaggerAnnotationFlag = Convert.toBool(getRequest().getParameter("swaggerAnnotationFlag"), false);
        try {
            Map<String, String> codes = GenUtil.generatorJson(sourceType, className, packageName, jsonschema, true, jpaAnnotationFlag, false, swaggerAnnotationFlag);
            return success(codes.entrySet().stream().map(entry -> {
                Map<String, String> param = new HashMap<>();
                param.put("path", entry.getKey());
                param.put("code", entry.getValue());
                return param;
            }).collect(Collectors.toList()));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @GetMapping("/public/validator/option")
    @ApiOperation(value = "加载bootstrapValidator校验规则")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", value = "表单名称"),
            @ApiImplicitParam(name = "from", value = "请求端", allowableValues = "vue")
    })
    public AjaxResponse validator() {
        Map<String, Object> result;
        try {
            String tableName = getRequest().getParameter("tableName");
            String from = getRequest().getParameter("from");
            if (StringUtil.isNotEmpty(from)) {
                result = genService.getValidatorFields(tableName, "2", true);
            } else {
                result = genService.getValidatorFields(tableName, "2");
            }
            if(result.isEmpty()) {
                result = null;
            }
        } catch (Exception e) {
            result = null;
        }

        return success(result);
    }
}
